<?php

class SelfController extends BaseController {

    static $view_path = 'self_service_end';
    protected $service = null;
    protected $member = null;

    public function __construct()
    {

        $result = true;
        $loginMsg = 'success';

        if(is_null(Auth::user()) || !Auth::user()->isMember()){
            $userId = Input::get('user_id');
            $token = Input::get('token');

            $memberService = new SelfMemberService();
            $repo = App::make('UserRepository');
            $input = $memberService->getUserInfo($userId, $token);


            if (!$repo->login($input)) {
                if($userId !== null && !$memberService->checkSignature($userId, $token)){
                    $loginMsg = '用户认证失败';
                    $result = false;
                }else {
                    $userInfo = $memberService->createMember();
                    if($userInfo === false){
                        $loginMsg = '新用户创建失败'.var_dump($memberService->errors()->getMessages());
                        $result = false;
                    }

                    if ($repo->login($userInfo)) {
                        $loginMsg = 'success';
                        $result = true;
                    }
                }
            }
        }
        if(!$result){
            echo $loginMsg;
            return $result;
        }
        $this->member = Auth::user()->attach;

        parent::__construct();
    }
}
